﻿package 
{
    public class Room extends MazeElement
    {
        private var m_number:uint;
        private var m_roomObjs:Array = new Array();

        public function Room(number:uint)
        {
            super();
            m_number = number;

            graphics.beginFill(0xaaaa);
            graphics.drawRect(0, 0, GameConstants.WIDTH, GameConstants.HEIGHT);
            graphics.endFill();
        }

        public function get roomNumber():uint
        {
            return m_number;
        }

        public function setSide(side:String, roomObj:Wall):void
        {
            m_roomObjs[side] = roomObj;
        }

        public function showRoom():void
        {
            // TODO: если не добавлены все объекты в комнате, то возникнет ошибка
            addRoomObjects(GameConstants.NORTH, m_roomObjs[GameConstants.NORTH]);
            addRoomObjects(GameConstants.SOUTH, m_roomObjs[GameConstants.SOUTH]);
            addRoomObjects(GameConstants.EAST, m_roomObjs[GameConstants.EAST]);
            addRoomObjects(GameConstants.WEST, m_roomObjs[GameConstants.WEST]);
        }

        private function addRoomObjects(side:String, roomObj:Wall):void
        {
            if (side == GameConstants.NORTH)
            {
                roomObj.x = 0;
                roomObj.y = 0;       
            }
            else if (side == GameConstants.WEST)
            {
                roomObj.rotation = 90;
                roomObj.x = 0;
                roomObj.y = (GameConstants.HEIGHT - roomObj.height) / 2;
            }
            else if (side == GameConstants.SOUTH)
            {
                roomObj.x = (GameConstants.WIDTH - roomObj.width) / 2
                roomObj.y = GameConstants.HEIGHT;                
            }
            else if (side == GameConstants.EAST)
            {
                roomObj.rotation = 90;
                roomObj.x = GameConstants.WIDTH;
                roomObj.y = (GameConstants.HEIGHT - roomObj.height) / 2;                
            }
            else
            {
                throw ArgumentError("Incorrect side");
            }

            addChild(roomObj);
        }

        public function isWallWithDoor(side:String):Boolean
        {
            if (m_roomObjs[side] == null)
            {
                return false;                
            }
            return m_roomObjs[side] is WallWithDoor;
        }

        public function getWallWithDoor(side:String):Wall
        {
            if (isWallWithDoor(side))
            {
                return m_roomObjs[side];
            }
            else
            {
                throw new ArgumentError("Incorrect side");
            }
                
        }
    }
}
